package com.coustemview.qianfeng.huaqiangbeistore.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.adapter.oversear_viewpagerevalute_adapter;
import com.coustemview.qianfeng.huaqiangbeistore.fragment.Fragment_oversear_shopdetails1;
import com.coustemview.qianfeng.huaqiangbeistore.fragment.Fragment_oversear_shopdetails2;
import com.coustemview.qianfeng.huaqiangbeistore.fragment.Fragment_oversear_shopdetails3;

import java.util.ArrayList;
import java.util.List;

public class Oversear_ShopdetailsActivity extends AppCompatActivity {
   private ImageView image_oversear_shopdetails;
    private ViewPager viewpager_oversear_shopdetails;
    private RadioButton[]radio;
    private RadioGroup mRadioGroup;
    private List<Fragment>mFragments;
    private FragmentPagerAdapter mPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_oversear__shopdetails);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setData() {
      viewpager_oversear_shopdetails.setAdapter(mPagerAdapter);
    }

    private void initData() {
        mFragments=new ArrayList<>();
       radio=new RadioButton[3];
        for (int i = 0; i <radio.length ; i++) {
            radio[i]=(RadioButton)mRadioGroup.getChildAt(i);
            radio[i].setChecked(false);
        }
        radio[0].setChecked(true);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < radio.length; i++) {
                    if (radio[i].getId() == checkedId) {
                        viewpager_oversear_shopdetails.setCurrentItem(i);
                    }
                }
            }
        });
       mFragments.add(new Fragment_oversear_shopdetails1());
        mFragments.add(new Fragment_oversear_shopdetails2());
        mFragments.add(new Fragment_oversear_shopdetails3());
        mPagerAdapter=new oversear_viewpagerevalute_adapter(getSupportFragmentManager(),mFragments);
    }

    private void setListener() {
        image_oversear_shopdetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

       viewpager_oversear_shopdetails.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
           @Override
           public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

           }

           @Override
           public void onPageSelected(int position) {

               for (int i = 0; i <radio.length ; i++) {
                   radio[i].setChecked(false);
               }
               radio[position].setChecked(true);
           }

           @Override
           public void onPageScrollStateChanged(int state) {

           }
       });
    }

    private void initView() {
       image_oversear_shopdetails= (ImageView) findViewById(R.id.image_oversear_shopdetailsback);
        viewpager_oversear_shopdetails= (ViewPager) findViewById(R.id.viewpager_oversear_shopdetails);
        mRadioGroup= (RadioGroup) findViewById(R.id.radiogroup_voersear_shopdetails);
    }
}
